#include <stdio.h>

#define ONE_GROUP_SIZE 1024

typedef unsigned char BYTE;

int bz2_decompressbuff(BYTE* buff, int size);

int main()
{
    BYTE test_data[ONE_GROUP_SIZE] = { 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26 };
    int test_size = sizeof(test_data);

    BYTE* compressed_data = NULL;
    compressed_data = malloc(ONE_GROUP_SIZE);
    memset(compressed_data, 0, ONE_GROUP_SIZE);

    int small = 1;
    int verbosity = 0;
    int ret = BZ2_bzBuffToBuffCompress((char*)compressed_data, (unsigned int*)&test_size, (char*)test_data, test_size, small, verbosity);
    if (ret != BZ_OK)
    {
        printf("compression failed: %d\n", ret);
        return 0;
    }

    //解压缩压缩后的数据
    BYTE* decompressed_data = NULL;
    decompressed_data = malloc(ONE_GROUP_SIZE);
    memset(decompressed_data, 0, ONE_GROUP_SIZE);

    int decompress_len = bz2_decompressbuff(compressed_data, test_size);
    if (decompress_len == 0)
    {
        printf("decompression failed\n");
        return 0;
    }

    //验证解压缩后的数据
    if (memcmp(test_data, decompressed_data, test_size) == 0)
    {
        printf("decompression succeeded\n");
    }
    else
    {
        printf("decompression failed\n");
    }

    //释放内存空间
    free(compressed_data);
    free(decompressed_data);

    return 0;
}